package com.dexilaonian.adpter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dexilaonian.R;
import com.dexilaonian.activity.EditActivity;
import com.dexilaonian.activity.ServiceActivity;
import com.dexilaonian.activity.StoreDetailActivity;
import com.dexilaonian.bean.ServiceBean;
import com.dexilaonian.utils.Constant;
import com.dexilaonian.utils.Logutil;
import com.dexilaonian.utils.Okhttp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
public class ServiceAdapter extends BaseAdapter{
    private Context mContext;
    private List<ServiceBean> mNewList;
    private int selectedPosition = 0;


    public void setSelectedPosition(int position) {
        selectedPosition = position;
    }

    public ServiceAdapter(Context context, List<ServiceBean> newList) {
        mContext =context;
        mNewList = newList;
    }

    @Override
    public int getCount() {
        return mNewList.size();
    }

    @Override
    public Object getItem(int position) {
        return mNewList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_service, null);
            viewHolder.sevice_name= (TextView) convertView.findViewById(R.id.my_servic_name);
            viewHolder.sevice_phone= (TextView) convertView.findViewById(R.id.tvphone);
            viewHolder.sevice_adress= (TextView) convertView.findViewById(R.id.service_dizhi);
            viewHolder.bianji= (TextView) convertView.findViewById(R.id.service_bianji);
            viewHolder.delete= (TextView) convertView.findViewById(R.id.shanchu);
            viewHolder.llServiceItem= (LinearLayout) convertView.findViewById(R.id.ll_service_item);
            viewHolder.address= (TextView) convertView.findViewById(R.id.service_dizhi);
            viewHolder.serviceSex = (ImageView) convertView.findViewById(R.id.service_sex);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.sevice_name.setText(mNewList.get(position).getRealName());
        viewHolder.sevice_phone.setText(mNewList.get(position).getTel());
        String address = mNewList.get(position).getAddress();
        if (address == null) {
            address = "";
        }
        viewHolder.sevice_adress.setText(mNewList.get(position).getProvince()+mNewList.get(position).getCity()+address);
        final int customer_id = mNewList.get(position).getCustomer_id();
        if (selectedPosition == position) {
            viewHolder.llServiceItem.setBackgroundColor(mContext.getResources().getColor(R.color.main_a9));
        } else {
            viewHolder.llServiceItem.setBackgroundColor(Color.TRANSPARENT);
        }
        viewHolder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Map<String, String> map = new HashMap<String, String>();
                map.put("customerId", customer_id +"");
                String delete_url = Constant.base_url + "/customer/remove_customer.do";
                Okhttp.postString(true, delete_url, map, new Okhttp.CallBac() {
                    @Override
                    public void onError(Call call, Exception e, String state, int id) {

                    }
                    @Override
                    public void onResponse(String response, int id) {
                        mNewList.remove(mNewList.get(position));
                        notifyDataSetChanged();
                    }
                    @Override
                    public void onNoNetwork(String state) {

                    }
                });
            }
        });
        viewHolder.bianji.setOnClickListener(new View.OnClickListener() {
            private Intent intent;
            @Override
            public void onClick(View v) {
                String customer_id = String.valueOf(mNewList.get(position).getCustomer_id());
                String address = mNewList.get(position).getAddress();
                String realName = mNewList.get(position).getRealName();
                String tel = mNewList.get(position).getTel();
                String city = mNewList.get(position).getCity();
                String province = mNewList.get(position).getProvince();
                int gender = mNewList.get(position).getGender();
                String medicalHistory = mNewList.get(position).getMedicalHistory();
                Long birthday =mNewList.get(position).getBirthday();
                String relation = mNewList.get(position).getRelation();
                String idcard = mNewList.get(position).getIdCard();
                intent = new Intent(mContext, EditActivity.class);
                intent.putExtra("realName",realName);
                intent.putExtra("phone",tel);
                intent.putExtra("province",province);
                intent.putExtra("city",city);
                intent.putExtra("address",address);
                intent.putExtra("customerId", customer_id);
                Bundle bundle = new Bundle();
                bundle.putInt("gender",gender);
                intent.putExtra("medicalHistory", medicalHistory);
                bundle.putLong("birthday", birthday);
                intent.putExtra("relation", relation);
                intent.putExtra("idcard", idcard);
                intent.putExtras(bundle);
                mContext.startActivity(intent);

            }
        });
        int gender = mNewList.get(position).getGender();
        if (gender == 1) {
            viewHolder.serviceSex.setImageResource(R.drawable.nan);
        } else {
            viewHolder.serviceSex.setImageResource(R.drawable.nv);
        }
        return convertView;
    }
    class ViewHolder{
        LinearLayout llServiceItem;
        TextView sevice_name;
        TextView sevice_phone;
        TextView sevice_adress;
        TextView bianji;
        TextView delete;
        TextView address;
        ImageView serviceSex;
    }
}
